package cn.tedu.project.oa.repair.controller;

import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import cn.tedu.project.oa.repair.pojo.dto.RepairFinish;
import cn.tedu.project.oa.repair.pojo.dto.RepairQuery;
import cn.tedu.project.oa.repair.pojo.dto.RepairSaveParam;
import cn.tedu.project.oa.repair.pojo.vo.RepairVO;
import cn.tedu.project.oa.repair.service.RepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/oa/asset/repair")
@Api(tags = "04.固定资产-维修模块")
public class RepairController {

    @Autowired
    RepairService repairService;

    @ApiOperation("保存维修单")
    @PostMapping("save")
    public JsonResult saveRepair(RepairSaveParam repairSaveParam){
        log.debug("保存维修单:repairSaveParam={}",repairSaveParam);

        repairService.saveRepair(repairSaveParam);

        return JsonResult.ok();
    }

    @ApiOperation("查询维修单列表")
    @GetMapping("select")
    public JsonResult selectList (RepairQuery repairQuery){
        log.debug("查询维修列表:repairQuery={}",repairQuery);

        List<RepairVO> list =repairService.selectList(repairQuery);

        return JsonResult.ok(list);
    }

    @ApiOperation("查看维修单详情")
    @GetMapping("show")
    public JsonResult showSelect(Long id){
        log.debug("查看维修单详情:id={}",id);

        RepairVO repairVO = repairService.selectById(id);

        return JsonResult.ok(repairVO);
    }

    @ApiOperation("完成维修处理")
    @PostMapping("finish")
    public JsonResult finish(RepairFinish repairFinish){
        log.debug("完成维修处理:repairFinish={}",repairFinish);

        repairService.updateFinish(repairFinish);

        return JsonResult.ok();
    }

    @ApiOperation("删除维修单")
    @PostMapping("delete/{id}")
    public JsonResult deleteRepair(@PathVariable Long id){
        log.debug("删除维修单:id={}",id);

        repairService.deleteRepair(id);

        return JsonResult.ok();
    }

    @ApiOperation("更新审批状态")
    @PostMapping("update")
    public JsonResult updateStatusById(RepairSaveParam repairSaveParam){
        log.debug("更新审批状态: repairSaveParam={}",repairSaveParam);

        repairService.updateStatusById(repairSaveParam);

        return JsonResult.ok();
    }

    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "5";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}
